package com.lemon.smartsafety.ui.activity.voice;

import android.media.MediaRecorder;
import android.widget.ImageView;

import com.lemon.smartsafety.R;

import java.io.IOException;

/**
 * Created by Cooliean on 16/4/12.
 */
public class AudioRecorder {
    private static int SAMPLE_RATE_IN_HZ = 8000; // 采样率

    private MediaRecorder mMediaRecorder;
    private String        mPath;

    public AudioRecorder(String path) {
        if (mMediaRecorder == null) {
            mMediaRecorder = new MediaRecorder();
        }
        this.mPath = path;
    }


    public void start() {
        if (this.mPath == null) {
            return;
        }
        try {
            mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
            mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            // recorder.setAudioChannels(AudioFormat.CHANNEL_CONFIGURATION_MONO);
            mMediaRecorder.setAudioSamplingRate(SAMPLE_RATE_IN_HZ);
            mMediaRecorder.setOutputFile(mPath);
            mMediaRecorder.prepare();
            mMediaRecorder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void stop() {
        try {
            mMediaRecorder.stop();
            mMediaRecorder.release();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public double getAmplitude() {
        if (mMediaRecorder != null) {
            int result = 0;
            try {
                result = mMediaRecorder.getMaxAmplitude();
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                return result;
            }
        } else return 0;
    }


    // 录音Dialog图片随声音大小切换
    public static void voiceImageChange(double voiceValue, ImageView mIvDialogRecVolume) {
        if (voiceValue < 600.0) {
            mIvDialogRecVolume.setImageResource(R.drawable.record_animate_01);
        } else if (voiceValue > 600.0 && voiceValue < 1000.0) {
            mIvDialogRecVolume.setImageResource(R.drawable.record_animate_02);
        } else if (voiceValue > 1000.0 && voiceValue < 1200.0) {
            mIvDialogRecVolume.setImageResource(R.drawable.record_animate_03);
        } else if (voiceValue > 1200.0 && voiceValue < 1400.0) {
            mIvDialogRecVolume.setImageResource(R.drawable.record_animate_04);
        } else if (voiceValue > 1400.0 && voiceValue < 1600.0) {
            mIvDialogRecVolume.setImageResource(R.drawable.record_animate_05);
        } else if (voiceValue > 1600.0 && voiceValue < 1800.0) {
            mIvDialogRecVolume.setImageResource(R.drawable.record_animate_06);
        } else if (voiceValue > 1800.0 && voiceValue < 2000.0) {
            mIvDialogRecVolume.setImageResource(R.drawable.record_animate_07);
        } else if (voiceValue > 2000.0 && voiceValue < 3000.0) {
            mIvDialogRecVolume.setImageResource(R.drawable.record_animate_08);
        } else if (voiceValue > 3000.0 && voiceValue < 4000.0) {
            mIvDialogRecVolume.setImageResource(R.drawable.record_animate_09);
        } else if (voiceValue > 4000.0 && voiceValue < 6000.0) {
            mIvDialogRecVolume.setImageResource(R.drawable.record_animate_10);
        } else if (voiceValue > 6000.0 && voiceValue < 8000.0) {
            mIvDialogRecVolume.setImageResource(R.drawable.record_animate_11);
        } else if (voiceValue > 8000.0 && voiceValue < 10000.0) {
            mIvDialogRecVolume.setImageResource(R.drawable.record_animate_12);
        } else if (voiceValue > 10000.0 && voiceValue < 12000.0) {
            mIvDialogRecVolume.setImageResource(R.drawable.record_animate_13);
        } else if (voiceValue > 12000.0) {
            mIvDialogRecVolume.setImageResource(R.drawable.record_animate_14);
        }
    }
}
